//package org.nbict.iot.task.once.schedule;
//
//import com.mongodb.client.model.Filters;
//import org.bson.Document;
//import org.bson.conversions.Bson;
//import org.nbict.iot.persist.pool.DbRepository;
//import org.nbict.iot.persist.repo.MongoRepo;
//import org.nbict.iot.task.once.Pointer;
//import org.nbict.iot.task.once.TimeSpanWindow;
//import org.nbict.iot.task.once.journey.JourneyStatus;
//
//import java.util.ArrayList;
//import java.util.List;
//
///**
// * Created by songseven on 18/6/27.
// */
//@Deprecated
//public class JourneyScheduler {
//
//    private MongoRepo repo;
//
//    public JourneyScheduler() {
//        repo = (MongoRepo) DbRepository.MONGO.getInstance();
//    }
//
//    private Window compute4single(String vin, Long from) {
//        if (vin == null) return null;
//        if (from == null) from = 0l;
//
//        //车辆查询条件
//        List filters = new ArrayList<Bson>();
//
//        filters.add(Filters.eq("car_vin", vin));
//        filters.add(Filters.gt("timestamp", from));
////        if (to > 0l) {
////            filters.add(Filters.lt("timestamp", to));
////        }
//
//        List<Document> result = repo.list("car_history", Filters.and(filters), null); //sortnew BasicDBObject("timestamp", 1) , Sorts.ascending("timestamp")
//
//        List<Pointer> pointers =  new ArrayList();
//        result.forEach(o -> {
//            Pointer p;
//            JourneyStatus status = null;
//            Long timestamp = o.getLong("timestamp");
//            int action = o.getInteger("action");
//            switch (action) {
//                case 1:
//                    status = JourneyStatus.LOGIN;
//                    break;
//                case 2:
//                case 3:
//                    Document uplaod = (Document) o.get("upload");
//                    Document car = (Document) uplaod.get("01");
//                    Document gis = (Document) uplaod.get("05");
//
//                    if (car == null || gis == null) break;
//                    String s = car.getString("status");
//                    if (s.equals("01")) {
//                        status = JourneyStatus.START;
//                    } else if (s.equals("02")) {
//                        status = JourneyStatus.END;
//                    }
//                    break;
//                case 4:
//                    status = JourneyStatus.LOGOUT;
//                    break;
//            }
//
//            p = new Pointer(timestamp, status);
//            pointers.add(p);
//        });
//
//        TimeSpanWindow journey = new TimeSpanWindow(vin);
//        pointers.forEach(pointer -> {journey.take(pointer);});
//
//        List<Document> res = new ArrayList<Document>();
//
//        journey.holder().forEach(timeSpan -> {
//            res.add(timeSpan.toDocument());
//            System.out.println(timeSpan);
//        });
//        System.out.println(journey.holder().size());
//
//        if (res.isEmpty()) return null;
//
//        return new Window(res,
//                journey.holder().get(journey.holder().size()-1).getTo());
//    }
//
//    public void record() {
//        //List cars = repo.list("car_schedule_offset", null, null);
//        List cars = repo.list("vehicle_map", null, null);
//        //List spans = new ArrayList<Document>();
//
//        cars.forEach(car -> {
//            Window window = compute4single(((Document) car).getString
//                    ("car_vin"), 0l); //((Document) car).getLong("last_batch_timestamp")
//            if (window != null) {
//                //repo.insert("car_run_journey", window.spans, false); //插入记录
//            }
//            //spans.addAll(window.spans);
//        });
////        repo.upsert("car_schedule_offset", )
//    }
//
//    public static void main(String[] args) {
//        new JourneyScheduler().record();
//    }
//
//    static final class Window {
//        List<Document> spans;
//        Long lastTimestamp;
//
//        public Window(List<Document> spans, Long lastTimestamp){
//            this.spans = spans;
//            this.lastTimestamp = lastTimestamp;
//        }
//    }
//}
